/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'ckeditor',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog,ckeditor ,datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            new Model({
                container: 'main-view',
                template: 'add-tpl',
                tag_name: 'div',
                url: _global.url.api + 'custom',
                callback: function () {
                    loaded();
                }
            }).fetch();
            $('#share_content').ckeditor({toolbar: []});
        },

        event: function () {
            this.data = {};
            var _this = this;
            //保存
            $("#btn-save").on('click', function (e) {
                _this.data = {};
                e.preventDefault();
                _this.data.img = $("input[name='imgs']").val();
                if (_this.data.img == '' || _this.data.img == undefined) {
                    Alert({msg: '请传入一张图片', type: 'error'});
                    return false;
                }
                _this.data.qrcode_width = $("#qrcode_width").val();
                if (_this.data.qrcode_width == '') {
                    Alert({msg: '请输入二维码宽度', type: 'error'});
                    return false;
                }
                _this.data.qrcode_height = $("#qrcode_height").val();
                if (_this.data.qrcode_height == '') {
                    Alert({msg: '请输入二维码高度', type: 'error'});
                    return false;
                }
                _this.data.qrcode_top = $("#qrcode_top").val();
                if (_this.data.qrcode_top == '') {
                    Alert({msg: '请输入距离头部的距离', type: 'error'});
                    return false;
                }
                _this.data.qrcode_left = $("#qrcode_left").val();
                if (_this.data.qrcode_left == '') {
                    Alert({msg: '请输入距离左边的距离', type: 'error'});
                    return false;
                }
                _this.data.avatar_width = $("#avatar_width").val();
                if (_this.data.avatar_width == '') {
                    Alert({msg: '请输入小头像宽度', type: 'error'});
                    return false;
                }
                _this.data.intro = $("#intro").val();
                if (_this.data.intro == '') {
                    Alert({msg: '请输入配置说明', type: 'error'});
                    return false;
                }
                _this.data.share_content = $("#share_content").val();
                if (_this.data.share_content == '') {
                    Alert({msg: '请输入分享文案', type: 'error'});
                    return false;
                }
                if (new Model({
                        url: _global.url.api + 'custom',
                        data: _this.data
                    }).post()) {
                    _this.fetch();
                }
            });
            //上传图片
            $('body').on('click', '#btn-upload', function (e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },

        imgPreview: function (img) {
            console.log('this.data', this.data);
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view').html(template('img-tpl', img));
            }
        }
    };

    new index();

});